import { userlist, usercount } from '../../../request/api'
interface userState {
    userList: Array<object>,
    pageSize: Number,
    count: Number,
    pageNum: Number
}
const state = {
    userList: [],
    pageSize: 2,
    pageNum: 1,
    count: 0
}
const getters = {
    getUser(state: userState) {
        return state.userList
    },
    getSize(state: userState) {
        return state.pageSize
    },
    getCount(state: userState) {
        return state.count
    }
}
const mutations = {
    REQ_USERLIST(state: userState, payload: any) {
       return( state.userList = payload)
    },
    REQ_COUNT(state: userState, payload: any) {
       return (state.count = payload)
    },
    REQ_PAGENUM(state: userState, payload: any) {
       return (state.pageNum = payload)
    }
}
const actions = {
    getUserAction(context: any) {
        // console.log(context, 'context');
        userlist({
            size: context.state.pageSize,
            page: context.state.pageNum
        }).then((res: any) => {
            // console.log(res, '////////');

            let list = res.data.list ? res.data.list : []
            if (context.state.pageNum != 1 && list.length == 0) {
                context.dispatch('changePageAction', context.state.pageNum - 1)
            }
            context.commit('REQ_USERLIST', res.data.list)
            // console.log(res.data.list);

        })
    },
    // 总条数
    getCountAction({ commit }: any) {
        usercount().then((res: any) => {
            // console.log(res,'count');
            commit('REQ_COUNT', res.data.list[0].total)
        })
    },
    // 修改总页码的
    changePageAction(context: any, payload: any) {
        context.commit('REQ_PAGENUM', payload)
        context.dispatch('getUserAction')
    }
}
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}